// pages/post-detail/post-detail.js
// 导入Data
import {
  postList
} from "../../data/data";
const app = getApp()

// 获取全局遍历App.js
// const app = getApp();
// console.log(app.global);

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // postPid: postList
    postPid: {},
    collected: false,
    _pid: null,
    _pCollected: {},
    _bgm: null,
    isPlaying: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (op) {
    const postPid = postList[op.pid];

    // pid在post.js中定义了
    this.data._pid = op.pid;
    this.data._pCollected = wx.getStorageSync('posts_collected')
    const pCollected = this.data._pCollected
    let collected = pCollected[this.data._pid]

    //判断（否则出错）
    if (pCollected == '') {
      this.data._pCollected = {}
    } else {
      this.data._pCollected = pCollected
    }

    if (collected === undefined) {
      collected = false
    }

    this.setData({
      // postDetail: postList,
      collected,
      postPid,
      isPlaying: this.currentMusicPlaying(),
    });

    const bgm = wx.getBackgroundAudioManager();
    this.data._bgm = bgm
    bgm.onPlay(this.onMusicStart)
    bgm.onStop(this.onMusicStop)

  },

  async onCollect() {

    // 收藏文章
    const postsCollected = this.data._pCollected;
    wx.getStorageSync('key')
    const collectedAnti = !this.data.collected

    const result = await wx.showModal({
      title: "是否收藏",
    })
    if (result.confirm) {
      postsCollected[this.data._pid] = collectedAnti;
      wx.showToast({
        title: collectedAnti ? "收藏成功" : "取消收藏",
        duration: 2000
      })

      this.setData({
        collected: postsCollected[this.data._pid]
      });

    }

    wx.setStorageSync('posts_collected', postsCollected)
  },

  // 分享(异步)
  async onShare() {
    const res = await wx.showActionSheet({
      itemList: ['分享到QQ', '分享到微信', '分享到微博'],
    })
    const tapIndex = res.tapIndex

    switch (tapIndex) {
      case 0:
        wx.showToast({
          title: "已成功分享到QQ",
          duration: 1500
        })
        break;
      case 1:
        wx.showToast({
          title: "已成功分享到微信",
          duration: 1500
        })
        break;
      case 2:
        wx.showToast({
          title: "已成功分享到微博",
          duration: 1500
        })
        break;

      default:
        break;
    }

  },

  currentMusicPlaying() {
    if (app.gPlayingMusic && app.gPlayingId === this.data._pid) {
      return true
    } else {
      return false
    }
  },

  onMusicStart(event) {
    const bgm = this.data._bgm;
    const musicId = postList[this.data._pid].music

    bgm.src = musicId.url;
    bgm.title = musicId.title;
    bgm.coverImgUrl = musicId.coverImg

    app.gPlayingMusic = true;
    app.gPlayingId = this.data._pid;

    this.setData({
      isPlaying: true
    })

  },
  onMusicStop(event) {
    const bgm = wx.getBackgroundAudioManager();
    bgm.stop();
    // bgm.pause();
    app.gPlayingMusic = false;
    app.gPlayingId = -1

    this.setData({
      isPlaying: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})